package _15_generics._15_10;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 10896390
 * @Date 2024/7/1 19:19
 * @Version 1.0
 */
public class _05_GenericReading {
    static <T> T readExact(List<T> list) {
        return list.get(0);
    }

    static List<Apple> apples = Arrays.asList(new Apple());
    static List<Fruit> fruit = Arrays.asList(new Fruit());

    static void f1() {
        Apple a = readExact(apples);
        Fruit f = readExact(fruit);
        f = readExact(apples);
    }

    static class Reader<T> {
        T readExact(List<T> list) {
            return list.get(0);
        }
    }

    static void f2() {
        Reader<Fruit> fruitReader = new Reader<>();
        Fruit f = fruitReader.readExact(_05_GenericReading.fruit);
//        fruitReader.readExact(apples);
    }

    static class CovariantRead<T> {
        T readCovariant(List<? extends T> list){
            return list.get(0);
        }
    }

    static void f3(){
        CovariantRead<Fruit> fruitReader = new CovariantRead<>();
        Fruit f = fruitReader.readCovariant(_05_GenericReading.fruit);
        Fruit fruit = fruitReader.readCovariant(apples);
    }
}
